/**
 * @(#)OssUtils.java 2016年8月18日
 * Copyright(C) 2015 ZHEXIN IT CO.,LTD. All rights reserved.
 */
package org.jsbd.boss.util;

import java.io.*;
import java.util.*;

import org.apache.commons.io.IOUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.aliyun.oss.OSSClient;
import com.aliyun.oss.model.ListObjectsRequest;
import com.aliyun.oss.model.OSSObject;
import com.aliyun.oss.model.OSSObjectSummary;
import com.aliyun.oss.model.ObjectListing;
import com.aliyun.oss.model.PutObjectResult;

/**
 * TODO 请加入本类的说明
 *
 * @author <a href="mailto:zhaihuilin@zhexinit.com" >翟惠林</a>
 * @version 1.0.0
 */
public class OssUtils {
    public static final Logger LOGGER = LoggerFactory.getLogger(OssUtils.class);

    // endpoint是访问OSS的域名。如果您已经在OSS的控制台上 创建了Bucket，请在控制台上查看域名。
    // 如果您还没有创建Bucket，endpoint选择请参看文档中心的“开发人员指南 > 基本概念 > 访问域名”，
    // 链接地址是：https://help.aliyun.com/document_detail/oss/user_guide/oss_concept/endpoint.html?spm=5176.docoss/user_guide/endpoint_region
    // endpoint的格式形如“http://oss-cn-hangzhou.aliyuncs.com/”，注意http://后不带bucket名称，
    // 比如“http://bucket-name.oss-cn-hangzhou.aliyuncs.com”，是错误的endpoint，请去掉其中的“bucket-name”。
    private static final String ENDPOINT = PropertiesUtil.getValue("oss.endpoint");

    // accessKeyId和accessKeySecret是OSS的访问密钥，您可以在控制台上创建和查看，
    // 创建和查看访问密钥的链接地址是：https://ak-console.aliyun.com/#/。
    // 注意：accessKeyId和accessKeySecret前后都没有空格，从控制台复制时请检查并去除多余的空格。
    private static final String ACCESS_KEY_ID = PropertiesUtil.getValue("oss.AccessKeyID");
    private static final String ACCESS_KEY_SECRET = PropertiesUtil.getValue("oss.AccessKeySecret");

    // Bucket用来管理所存储Object的存储空间，详细描述请参看“开发人员指南 > 基本概念 > OSS基本概念介绍”。
    // Bucket命名规范如下：只能包括小写字母，数字和短横线（-），必须以小写字母或者数字开头，长度必须在3-63字节之间。
    private static final String BUCKET_NAME = "qy-crack";


    private static final OSSClient OSS_CLIENT = new OSSClient(ENDPOINT, ACCESS_KEY_ID, ACCESS_KEY_SECRET);

    public static void uploadFile(String filename, String value) throws IOException {
        if (value == null) {
            return;
        }
        InputStream is = new ByteArrayInputStream(value.getBytes());
        PutObjectResult result = OSS_CLIENT.putObject(BUCKET_NAME, filename, is);
        LOGGER.debug(result.getRequestId());
    }

    public static PutObjectResult uploadFile(String filename, InputStream in) {
        return OSS_CLIENT.putObject(BUCKET_NAME, filename, in);
    }

    public static void uploadFile(String bucketName, String filename, InputStream in) {
        OSS_CLIENT.putObject(bucketName, filename, in);
    }

    public static void uploadFile(String bucketName, String filename, byte[] data) {
        ByteArrayInputStream in = new ByteArrayInputStream(data);
        OSS_CLIENT.putObject(bucketName, filename, in);
    }

    public static byte[] downloadFile(String bucketName, String filename) throws IOException {
        OSSObject object = OSS_CLIENT.getObject(bucketName, filename);
        ByteArrayOutputStream bout = new ByteArrayOutputStream();
        IOUtils.copy(object.getObjectContent(), bout);
        return bout.toByteArray();
    }

    public static byte[] downloadFile(String filename) throws IOException {
        return downloadFile(BUCKET_NAME, filename);
    }

    public static byte[] downloadWebmengFile(String filename) throws IOException {
        return downloadFile("webmeng", filename);
    }


    public static void deleteFile(String fileName) {
        OSS_CLIENT.deleteObject(BUCKET_NAME, fileName);
    }

    /**
     * listObjects:获取oss文件
     *
     * @return
     * @author <a href="mailto:xupengcheng@zhexinit.com" >徐鹏程</a>
     */
    public static List<OSSObjectSummary> listQyCrackObjects(String prefix) {
        ObjectListing result = OSS_CLIENT.listObjects(BUCKET_NAME, prefix);
        return result.getObjectSummaries();
    }

    public static List<OSSObjectSummary> listObjects(String bucketName, String prefix, String maker) {
        ObjectListing result = OSS_CLIENT.listObjects(new ListObjectsRequest(bucketName, prefix, maker, null, 100));
        return result.getObjectSummaries();
    }


}
